#include <stdio.h>
#include <stdlib.h>
#include "BinarySortTree.h"


#define true 1;
#define false 0;
struct Student
{
    int id;
    char* name;   
};

struct Student* CreateStudent(int id, const char* name)
{
    struct Student* stu = (struct Student*)malloc(sizeof(struct Student));
    if(stu == NULL)
    {
        printf("CreateStudent malloc error!\n");
        return NULL;
    }
    stu->id = id;
    stu->name = (char*)name;  
    return stu; 
}

void FreeStudent(struct Student* stu)
{
    if(stu != NULL)
    {
        free(stu);
    }
}


int SortByID(ElementType value1, ElementType value2)
{
    struct  Student* stu1 = (struct Student*)value1;
    struct  Student* stu2 = (struct Student*)value2;
    if(stu1->id < stu2->id)
       {return true;}
    else
       {return false;}  
}

void Print(ElementType value)
{
    struct  Student* stu = (struct Student*)value;
    printf("stu id : %d ,stu name : %s\n", stu->id, stu->name);
}

int CompareID(ElementType value1, ElementType value2)
{
    struct  Student* stu1 = (struct Student*)value1;
    struct  Student* stu2 = (struct Student*)value2;
    return stu1->id - stu2->id;
}


int main()
{
   BSTree tree;
   BST_Init(&tree);

   BST_InsertElement(&tree, CreateStudent(2, "zhangsan"),SortByID);
   BST_InsertElement(&tree, CreateStudent(1, "lisi"),SortByID);
   BST_InsertElement(&tree, CreateStudent(3, "wangwu"),SortByID);
  
    //PrevTravel(&tree, Print);
    BST_MidTravel(&tree, Print);
    //PostTravel(&tree, Print);

    // struct  Student stu3 = {2, ""};
    // DeleteElement(&tree, &stu3, CompareID);
    // MidTravel(&tree, Print);
    
    // printf("tree number: %d\n", BST_GetLeafNUm(&tree));
    BST_SeqTravel(&tree, Print, NULL);

    BST_FreeTree(&tree, (void*)FreeStudent);

    return 0 ;
}